﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using KBManagement.DAL;
using KBManagement.BIZ;
using KBManagement.Library;
using CrystalDecisions.CrystalReports;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Windows;


namespace KBManagement
{
    public partial class PrintOrder : Form
    {

        KBSQLClassDataContext context;
        public PrintOrder(IQueryable orderPrint, Purchase_Order pOrder, DateTime EndTime,String nameCustomer,String total)
        {
            System.Data.DataTable myTable = new DataTable("myTable");
            System.Data.DataColumn column;
            System.Data.DataRow row;

            //create 3 columns
            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "Name";
            column.ReadOnly = true;

            // Add the Column to the DataColumnCollection.
            myTable.Columns.Add(column);
            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "Fo_Name";
            column.ReadOnly = true;

            // Add the Column to the DataColumnCollection.
            myTable.Columns.Add(column);
            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "FullName";
            column.ReadOnly = true;

            // Add the Column to the DataColumnCollection.
            myTable.Columns.Add(column);
            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "StartTime";
            column.ReadOnly = true;

            // Add the Column to the DataColumnCollection.
            myTable.Columns.Add(column);
            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "EndTime";
            column.ReadOnly = true;

            // Add the Column to the DataColumnCollection.
            myTable.Columns.Add(column);

            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "Sale_Rate";
            column.ReadOnly = true;

            // Add the Column to the DataColumnCollection.
            myTable.Columns.Add(column);


            column = new DataColumn();
            column.DataType = System.Type.GetType("System.Double");
            column.ColumnName = "Price";
            column.ReadOnly = true;

            // Add the Column to the DataColumnCollection.
            myTable.Columns.Add(column);
            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "Quantity";
            column.ReadOnly = true;

            // Add the Column to the DataColumnCollection.
            myTable.Columns.Add(column);
            column = new DataColumn();
            column.DataType = System.Type.GetType("System.Double");
            column.ColumnName = "Amount";
            column.ReadOnly = true;

            // Add the Column to the DataColumnCollection.
            myTable.Columns.Add(column);
            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "Purchase_Amount";
            column.ReadOnly = true;

            // Add the Column to the DataColumnCollection.
            myTable.Columns.Add(column);
            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "Purchase_Date";
            column.ReadOnly = true;

            // Add the Column to the DataColumnCollection.
            myTable.Columns.Add(column);

            column = new DataColumn();
            column.DataType = System.Type.GetType("System.Double");
            column.ColumnName = "Total_Amount";
            column.ReadOnly = true;

            // Add the Column to the DataColumnCollection.
            myTable.Columns.Add(column);


            context = Utils.CreateInstance().GetDBContext();
            InitializeComponent();
            bool flag = false;
            ReportDocument reportDocument = new ReportDocument();
            string path = Application.StartupPath;
            path = path.Substring(0, path.LastIndexOf("bin"));
            reportDocument.Load(path + "CrystalReport6.rpt");

            row = myTable.NewRow();
            row["Name"] = pOrder.Product.Name;
            row["Price"] = Double.Parse(pOrder.Product.Price.ToString());
            row["Sale_Rate"] = pOrder.Sale_Rate;
            row["FullName"] = nameCustomer;
            row["Purchase_Amount"] = 0;
            row["Purchase_Date"] = 0;
            row["Fo_Name"] ="--------------";
            row["StartTime"] = Convert.ToDateTime(pOrder.StartTime).ToShortTimeString();
            row["EndTime"] = Convert.ToDateTime(EndTime).ToShortTimeString();
            row["Quantity"] = "--------------";
            //row["Amount"] = "--------------";
            row["Total_Amount"] = Double.Parse(total);
            //add the row of data to the table
            Double kbTotal = 0.0;
                foreach (Order_Detail odetail in orderPrint)
                {
                    kbTotal = kbTotal + Convert.ToDouble(odetail.Amount);
                    row = myTable.NewRow();
                    row["Name"] = pOrder.Product.Name;
                    row["Price"] = Double.Parse(pOrder.Product.Price.ToString());
                    row["Sale_Rate"] = pOrder.Sale_Rate;
                    row["FullName"] = nameCustomer;
                    row["Purchase_Amount"] = Double.Parse(total) - kbTotal - pOrder.Sale_Rate;
                    row["Purchase_Date"] = 0;
                    row["StartTime"] = Convert.ToDateTime(pOrder.StartTime).ToShortTimeString();
                    row["EndTime"] = Convert.ToDateTime(EndTime).ToShortTimeString();
                    row["Fo_Name"] = odetail.Food.Fo_Name;
                    row["Quantity"] = odetail.Quantity;
                    row["Amount"] = Double.Parse(odetail.Amount.ToString());
                    row["Total_Amount"] = Double.Parse(total);
                    //add the row of data to the table
                    myTable.Rows.Add(row);
                    flag = true;
                }

                if (!flag)
                {
                    myTable.Rows.Add(row);
                }
            reportDocument.SetDataSource(myTable);
            crystalReportViewer1.ReportSource = reportDocument;            
        }
    }
}
